from rest_framework.views import APIView
from rest_framework.response import Response
from .services import SMSService
from .serializers import SMSSendSerializer, SMSVerifySerializer

class SMSSendView(APIView):
    def post(self, request):
        serializer = SMSSendSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        
        if SMSService.send_code(request, **serializer.validated_data):
            return Response({"status": "success"})
        return Response({"error": "发送频率过高"}, status=429)

class SMSVerifyView(APIView):
    def post(self, request):
        serializer = SMSVerifySerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        
        if SMSService.verify_code(request, **serializer.validated_data):
            return Response({"status": "verified"})
        return Response({"error": "验证码无效"}, status=400)